//获取应用实例
var app = getApp();
Page({
  data: {
    thumb: '',
    nickname: '',
    location:'',
    canSee:false,
    gender:'',
    linkmanGuid: '',
    phonenumber: ''
  },
  onShow() {
    let userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      app.globalData.nickname = userInfo.nickName,
        this.setData({
          thumb: userInfo.avatarUrl,
          nickname: userInfo.nickName,
          location: userInfo.country + "-" + userInfo.province + "-" + userInfo.city,
          gender: userInfo.gender
        })
      var userguid = wx.getStorageSync('OPEN_ID');
      if(userguid){
        getUserByUserGuid(this, userguid);
      }
    }else{
      wx.navigateTo({
        url: "../authorize/authorize"
      })
    }
  },
  toAddCategory: function (event) {
    wx.navigateTo({
      url: "../todo/todo"
    })
  },
  toAddYiJian: function (event) {
    wx.navigateTo({
      url: "../addYiJian/addYiJian"
    })
  },
  toAbout: function (event) {
    wx.navigateTo({
      url: "../about/about"
    })
  },
  toSetting:function(e){
    wx.navigateTo({
      url: "../setting/setting"
    })
  },
  toallUser:function(e){
    wx.navigateTo({
      url: "../allUser/allUser"
    })
  },
  tozan:function(event){
    wx.previewImage({
      current: 0, // 当前显示图片的http链接  
      urls: [app.globalData.zanUrl] // 需要预览的图片http链接列表  
    })  
    // wx.navigateTo({
    //   url: "../zan/zan"
    // })
  },
   toAddtxl: function (event) {
    wx.navigateTo({
      url: "../addCategory/addCategory"
    })
  },
  toAddlinkman: function (event) {
    wx.navigateTo({
      url: "../addLinkman/addLinkman"
    })
  },
  bindphone: function (event) {
    let that = this
    var phoneNumber = wx.getStorageSync('phoneNumber')
    this.setData({
      phonenumber: phoneNumber,
    })
    console.log(phoneNumber)
    if (!phoneNumber) {
      wx.navigateTo({
        url: "../authorize_phone/authorize_phone"
      })
    } else {
      //通过电话号码获取联系人信息
      getLinkmanByPhoneNum(that);
    }
  },
})
//获取数据
var getLinkmanByPhoneNum = function (self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'Linkman/getLinkmanByPhoneNum', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        linkmanTelephone: self.data.phonenumber
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        self.setData({
          linkmanGuid: res.data.UserArea.linkmanGuid,
        })
        wx.showModal({
          title: '提示',
          content: '该手机号：' + self.data.phonenumber + '在通讯录中属于' + res.data.UserArea.linkmanName + '，是否与之绑定？',
          confirmText: "确定",
          cancelText: "取消",
          success: function (res) {
            console.log(res);
            if (res.confirm) {
              console.log('用户点击主操作')
              BandTelephone(self);
            } else {
              console.log('用户点击辅助操作')
            }
          }
        });
      } else {
        wx.showModal({
          content: '该号码未在通讯录中使用，无法进行绑定。',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
      }
    }
  })
}
//绑定账号
var BandTelephone = function (self) {
  let userInfo = wx.getStorageSync('userInfo')
  var OPEN_ID = wx.getStorageSync('OPEN_ID')
  app.globalData.OPEN_ID = OPEN_ID;
  wx.request({
    url: app.globalData.ServiceUrl + 'Linkman/updateLinkman', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        linkmanGuid: self.data.linkmanGuid,
        userGuid: OPEN_ID,
        linkmanHeadUrl: userInfo.avatarUrl,
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        // wx.showModal({
        //   content: '绑定成功,您可以修改该用户的信息了',
        //   showCancel: false,
        //   success: function (res) {
        //     if (res.confirm) {
        //       console.log('用户点击确定')
        //     }
        //   }
        // });
        register(self);
      } else {
        wx.showModal({
          content: '绑定失败，原因不知',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
      }
    }
  })
}

var register = function (self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'user/register', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        userName: self.data.nickname,
        userGuid: app.globalData.OPEN_ID,
        photoUrl: self.data.thumb,
        sex: self.data.gender,
        adress: self.data.location,
        telephoneNum: self.data.phonenumber
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      // if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        wx.showModal({
          content: '绑定成功,您可以修改该用户的信息了',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
      // } else {
      //   wx.showModal({
      //     content: '绑定失败，原因不知',
      //     showCancel: false,
      //     success: function (res) {
      //       if (res.confirm) {
      //         console.log('用户点击确定')
      //       }
      //     }
      //   });
      // }
    }
  })
}

var getUserByUserGuid = function (self, userguid) {
  wx.request({
    url: app.globalData.ServiceUrl + 'user/getUserByUserGuid', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        userGuid: userguid,
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        if (res.data.UserArea.isAdmin == 1){
          self.setData({
            canSee:true
          })
        }
      }
    }
  })
}